package bairui.international.console.web.controller.personnel.employee;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.project.authority.model.type.Type_Operator;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.web.security.common.enums.EncryptionWay;
import com.lianjing.sh.web.security.common.util.Util_Encryption;
import com.sh_lianjing.util.Util_BeanCopyByDeclaredProperties;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.status.State_Employee;
import bairui.international.model.type.Type_Gender;
import bairui.international.service.Service_Admin;


/**
 * @generated
 *
 * @see Test_C_Personnel_Employee_Add 测试
 *
 */
@Controller
public class C_Personnel_Employee_Add
 extends SC_Personnel_Employee_Add 
{
	
	@Autowired @Lazy private Service_Admin service_Admin;
	
	/**
	 * 添加
	 */
	@Action(Add.class)
	@PostMapping(Add.RequestMappingValue)
	public @FomatOutput @ResponseBody Object add(
		@Valid Add.InputModel input
	) throws Exception {
		TB_Admin bean = new TB_Admin().properties(new TB_Admin.MustProperties() {
			@Override public String getUsername() {
				return input.getMobilePhone().trim();
			}
			@Override public State_Employee getState() {
				return State_Employee.t_聘用().to();
			}
			@Override public String getPassword() {
				try {
					return Util_Encryption.hashEncryption(input.getMobilePhone().trim(), new EncryptionWay[]{EncryptionWay.MD5});
				} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
					throw new RuntimeException(e);
				}
			}
			@Override public Type_Operator getOperatorType() {
				return Type_Operator.Admin;
			}
			@Override public String getOperatorName() {
				return input.getName();
			}
			@Override public String getNumber() {
				return input.getNumber();
			}
			@Override public String getName() {
				return input.getName();
			}
			@Override public String getMobilePhone() {
				return input.getMobilePhone();
			}
			@Override public Type_Gender getGender() {
				return input.getGender();
			}
			@Override public Boolean getFreezenFlag() {
				return false;
			}
			@Override public Boolean getChangeDefaultPasswordFlag() {
				return false;
			}
		});
		//
		bean = Util_BeanCopyByDeclaredProperties.copyProperties(input, bean);
		/*
		 * 设置默认用户名与密码
		 */
		//
		service_Admin.save(bean);
		//
		return null;
	}

}


